import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 引入常量
import * as CDATA from "./constant"

Vue.prototype.CDATA = CDATA

// 引入工具函数
import * as tools from "./utils/tools.js"

Vue.prototype.tools = tools

// 正则
import * as reg from "./utils/reg"

Vue.prototype.reg = reg

// loadsh
import _ from "lodash"

Vue.prototype._ = _

// 引入es6-promise，解决IE不支持的问题
require("es6-promise").polyfill()


// 初始化css
// pc
import "@/assets/css/pc/base.css"

// 引入animate.css
import "animate.css/animate.css"

// 按需加载element-UI
import {
    Message,
    Notification,
    Button,
    Form,
    FormItem,
    Input,
    Container,
    Aside,
    Header,
    Footer,
    Main,
    Menu,
    Submenu,
    MenuItem,
    MenuItemGroup
} from "element-ui"

Vue.use(Button)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Container)
Vue.use(Aside)
Vue.use(Header)
Vue.use(Footer)
Vue.use(Main)
Vue.use(Menu)
Vue.use(Submenu)
Vue.use(MenuItem)
Vue.use(MenuItemGroup)
Vue.prototype.$message = Message
Vue.prototype.$notify = Notification;

// 注册全局过滤器
import * as filter from '@/filters'
// 导出一个对象，循环注册在vue上
Object.keys(filter).forEach(key => {
    Vue.filter(key, filter[key])
})

// 图片懒加载
// 使用本地图片需require()
import vueLazyLoad from "vue-lazyload"

Vue.use(vueLazyLoad, {
    error: require("@/assets/img/error.jpg"),
    loading: require("@/assets/img/load4.gif")
})

// vue-quill-editor富文本编辑器
import VueQuillEditor from 'vue-quill-editor'

Vue.use(VueQuillEditor)

// 是否显示生产模式的消息
Vue.config.productionTip = false

// 请求文件
import api from "./api/api/index"

import NProgress from "nprogress";

router.beforeEach((to, from, next) => {
    if (to.path !== from.path) {
        // NProgress.start();
    }

    // 进登录页面时
    if (to.path === "/login") {
        // local中有用户名时，禁止跳转登录页面
        if (localStorage.getItem("user")) {
            next({
                path: "/"
            })
        }
    }

    // 判断该页面是否需要登录权限
    if (to.meta.requireAuth) {
        // 判断local中是否存在用户名
        if (localStorage.getItem("user")) {
            // 存在可以跳转
            next()
        } else {
            if (to.path === "/login") {
                next()
            } else {
                next({
                    path: "/login"
                })
            }
        }
    } else {
        next()
    }
    next();
});

router.afterEach(() => {
    // NProgress.done();
});

// 防止用户手动更改local
window.addEventListener("storage", function (e) {
    if (e.newValue !== e.oldValue || e.newValue === null) {
        router.push("/login")
    }
});

// 请求文件
Vue.prototype.$api = api

new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
